package com.bdqn.zhang.servlet;

import com.alibaba.fastjson.JSONObject;
import com.bdqn.zhang.pojo.*;
import com.bdqn.zhang.service.ServiceVivodlrizhi;
import com.bdqn.zhang.service.ServiceVivoly;
import com.bdqn.zhang.service.ServiveVivorz;
import com.bdqn.zhang.service.VivoglyService;
import com.bdqn.zhang.service.impl.ServiceVivodlrizhiimpl;
import com.bdqn.zhang.service.impl.ServiceVivolyimpl;
import com.bdqn.zhang.service.impl.ServiceVivorzimpl;
import com.bdqn.zhang.service.impl.VivoglyServiceimpl;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

@WebServlet(name = "servletVivoglycx", urlPatterns = {"/gly"})
//文件上传
@MultipartConfig
public class ServletVivoglycx extends HttpServlet {
    VivoglyService vivoglyService = new VivoglyServiceimpl();
    ServiceVivoly vivoly=new ServiceVivolyimpl();
    ServiveVivorz serviceVivorz=new ServiceVivorzimpl();
    ServiceVivodlrizhi serviveVivodlrizhi=new ServiceVivodlrizhiimpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理post传参乱码
        req.setCharacterEncoding("UTF-8");
        //处理响应乱码
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        //opr 判断不同的业务类型
        String opr = req.getParameter("opr");
        //equals判断常量在前 不会出现空指针异常
        boolean flag;
        if ("getAll".equals(opr)) {
            //获取当前页
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            //全查
            List<Vivogly> all = vivoglyService.getAll((int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
//            //获取表中所以的记录数
            int count = vivoglyService.getCount();
            LayData laydata = new LayData();
            laydata.setCode(0);
            laydata.setMsg("");
            laydata.setCount(count);
            laydata.setData(all);
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        } else if ("ztxg".equals(opr)) {
            String id = req.getParameter("id");
            String zhi = req.getParameter("zhi");
            int zhis = Integer.parseInt(zhi);
            int zt;
            if (zhis == 0) {
                zt = 1;
            } else {
                zt = 0;
            }
            int count = vivoglyService.gai(new Vivogly(id, zt));//匿名对象 调用完之后会被内存回收 省内存空间
            flag = false;
            if (count > 0) {
                flag = true;
            }
            resp.getWriter().write(String.valueOf(flag));
        } else if ("del".equals(opr)) {
            int i = 0;
            String id = req.getParameter("id");
            System.out.println(id);
            String[] ids = id.split(",");  //分割id，成一个字符串数组
            int n = ids.length;
            for (int j = 0; j < n; j++) {
                i = vivoglyService.del(ids[j]);       //循环执行删除
            }
            flag = false;
            if (i > 0) {
                flag = true;
            }
            resp.getWriter().write(String.valueOf(flag));


        } else if ("addSave".equals(opr)) {
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String sex = req.getParameter("sex");
            String zt = req.getParameter("zt");
            String phone = req.getParameter("phone");
            String zp = req.getParameter("photo");
            String xb;
            if ("1".equals(sex)) {
                xb = "男";
            } else {
                xb = "女";
            }
            Date date = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            String dates = df.format(date);
            // Formats a Date into a date/time string.// 2019-07-03
            String photo = "static/img/" + zp;
            int zhuangtai = Integer.parseInt(zt);
            int count = vivoglyService.add(new Vivogly(name, password, xb, photo, zhuangtai, phone));//匿名对象 调用完之后会被内存回收 省内存空间
            flag = false;
            if (count > 0) {
                flag = true;
            }
            resp.getWriter().write(String.valueOf(flag));
        } else if ("upload".equals(opr)) {
            String realPath = req.getSession().getServletContext().getRealPath("static");
            Part part = req.getPart("file");
            String fileName = part.getSubmittedFileName();
            AjaxObject object = new AjaxObject();
            object.setImgSrc(fileName);
            resp.getWriter().write(JSONObject.toJSONString(object));
        } else if ("addSave2".equals(opr)) {
            String id = req.getParameter("id");
            String name = req.getParameter("name");
            String phone = req.getParameter("phone");
            String sex = req.getParameter("sex");
            String zp = req.getParameter("photo");
            // Formats a Date into a date/time string.// 2019-07-03
            String photo = "static/img/" + zp;
            String zhuang = req.getParameter("zhuangtai");
            int zhuangtai = Integer.parseInt(zhuang);
            int count = vivoglyService.edits(new Vivogly(id, name,sex, photo, phone, zhuangtai));//匿名对象 调用完之后会被内存回收 省内存空间
            flag = false;
            if (count > 0) {
                flag = true;
            }
            resp.getWriter().write(String.valueOf(flag));
        }else if ("glyyhmcx".equals(opr)) {
            String p_name = req.getParameter("p_name");
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            String rr = req.getParameter("rr");
            LayData laydata = new LayData();
            if ("仅查询用户名".equals(rr)) {
                List<Vivogly> mh = vivoglyService.yhmcx(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
                int count = vivoglyService.mhdan(p_name);
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(count);
                laydata.setData(mh);
                String jsonString = JSONObject.toJSONString(laydata);
                resp.getWriter().write(jsonString);
            }  else if ("仅查询性别".equals(rr)) {
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(vivoglyService.mhdan(p_name));
                laydata.setData(vivoglyService.xbcx(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit)));
                String jsonString = JSONObject.toJSONString(laydata);
                resp.getWriter().write(jsonString);
            }
        }
        else if ("getAll2".equals(opr)) {
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            LayData laydata = new LayData();
            String p_name = req.getParameter("p_name");
            if (p_name != null && !" ".equals(p_name)) {
                List<Vivogly> mh = vivoglyService.dan(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
                int count = vivoglyService.mhdan(p_name);
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(count);
                laydata.setData(mh);
            }
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        } else if ("ztcx".equals(opr)) {
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            LayData laydata = new LayData();
            String p_name = req.getParameter("p_name");
            int name;
            if (p_name != null && !" ".equals(p_name)) {
                if ("启用".equals(p_name)) {
                    name = 1;
                } else {
                    name = 0;
                }
                List<Vivogly> mh = vivoglyService.ztcx(name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
                int count = vivoglyService.mhztcxs(name);
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(count);
                laydata.setData(mh);
            }
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        } else if ("dl".equals(opr)) {
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            int count = vivoglyService.dlcx(name, password);//匿名对象 调用完之后会被内存回收 省内存空间
            flag = false;
            if (count > 0) {
                flag = true;
            }
            resp.getWriter().write(String.valueOf(flag));
        } else if ("ctx".equals(opr)) {
            String name = req.getParameter("name");
            String count = vivoglyService.ctx(name);
            resp.getWriter().write(JSONObject.toJSONString(count));
        } else if ("checkName".equals(opr)) {
            String name = req.getParameter("name");
            int count = vivoglyService.checkNameUnique(name);
            flag = false;
            if (count > 0) {
                flag = true;
            }
            String jsonString = JSONObject.toJSONString(flag);
            resp.getWriter().write(jsonString);

        } else if ("kk".equals(opr)) {
            Pages pages = new Pages();
            //获取当前页
            String current = req.getParameter("current");
            int int_current = 0;
            if (current == null) {
                int_current = 1;
            } else {
                int_current = Integer.parseInt(current);
            }
            //获取表中所以的记录数
            int count = vivoglyService.getCount();
            pages.setTotalCount(count);
            pages.setTotalPage();//帮我们计算总页数
            pages.setCurrentPage(int_current);
            //全查
            List<Vivogly> all = vivoglyService.getAll((int_current - 1) * pages.getPageSix(),pages.getPageSix());
            req.getSession().setAttribute("all", all);
            req.getSession().setAttribute("pages", pages);
            resp.sendRedirect("welcome-2.jsp");
        }else if ("czl".equals(opr)) {
            String name = req.getParameter("name");
            List<Vivogly> alls = vivoglyService.czl(name);
            String datajson = JSONObject.toJSONString(alls);
            resp.getWriter().write(datajson);
        }else if ("glyly".equals(opr)) {
            String name = req.getParameter("name");
            String phone = req.getParameter("phone");
            String neirong = req.getParameter("neirong");
            name = name.replace("'", "");
            phone = phone.replace("'", "");
            neirong = neirong.replace("'", "");
           int zhuangtai=1;
            int count=vivoly.add(new Vivoly(name,phone,neirong,zhuangtai));
            flag = false;
            if (count > 0) {
                flag = true;
            }
            resp.getWriter().write(String.valueOf(flag));
        }else if ("lycx".equals(opr)) {
            //获取当前页
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            //全查
            List<Vivoly> all = vivoly.getAll((int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
//            //获取表中所以的记录数
            int count = vivoly.getCount();
            LayData laydata = new LayData();
            laydata.setCode(0);
            laydata.setMsg("");
            laydata.setCount(count);
            laydata.setData(all);
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        }else if ("delly".equals(opr)) {
            int i = 0;
            String id = req.getParameter("id");
            String[] ids = id.split(",");  //分割id，成一个字符串数组
            int n = ids.length;
            for (int j = 0; j < n; j++) {
                i = vivoly.del(ids[j]);       //循环执行删除
            }
            flag = false;
            if (i > 0) {
                flag = true;
            }
            resp.getWriter().write(String.valueOf(flag));
        }else if ("delrz".equals(opr)) {
            int i = 0;
            String id = req.getParameter("id");
            String[] ids = id.split(",");  //分割id，成一个字符串数组
            int n = ids.length;
            for (int j = 0; j < n; j++) {
                i = serviceVivorz.del(ids[j]);       //循环执行删除
            }
            flag = false;
            if (i > 0) {
                flag = true;
            }
            resp.getWriter().write(String.valueOf(flag));
        }
        else if ("hhh".equals(opr)) {
            String ids = req.getParameter("id");
            int id=Integer.parseInt(ids);
            String zhuangtais = req.getParameter("zhuangtai");
            int zhuangtaiss=Integer.parseInt(zhuangtais);
            int zhuangtai=0;
            if (zhuangtaiss==1){
                zhuangtai=0;
            }
            String ss = req.getParameter("zhi");
            ss = ss.replace("'", "");
            int count=vivoly.edits(new Vivoly(id,zhuangtai,ss));
            flag = false;
            if (count > 0) {
                flag = true;
            }
            resp.getWriter().write(String.valueOf(flag));
        }else if ("ztcxly".equals(opr)) {
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            LayData laydata = new LayData();
            String p_name = req.getParameter("p_name");
            if (p_name != null && !" ".equals(p_name)) {
                List<Vivoly> mh = vivoly.dan(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
                int count = vivoly.mhdan(p_name);
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(count);
                laydata.setData(mh);
            }
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        }else if ("xxl".equals(opr)) {
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            LayData laydata = new LayData();
            String p_name = req.getParameter("p_name");
            int name = 0;
            if (p_name != null && !" ".equals(p_name)) {
                if ("回复留言".equals(p_name)) {
                    name = 1;
                } else if ("已回复".equals(p_name)) {
                    name = 0;
                }
                List<Vivoly> mh = vivoly.ztcx(name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
                int count = vivoly.mhztcx(name);
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(count);
                laydata.setData(mh);
            }
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        }else if ("xmr".equals(opr)) {
            String name = req.getParameter("name");
            String cz = req.getParameter("cz");
            String gn = req.getParameter("gn");
            String jg = req.getParameter("jg");
            Date date = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String dates = df.format(date);
            int count = serviceVivorz.add(new Vivorz(name,cz,gn,dates,jg));//匿名对象 调用完之后会被内存回收 省内存空间
        }else if ("ggd".equals(opr)) {
            String name = req.getParameter("name");
            //获取当前页
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            //全查
            List<Vivorz> all = serviceVivorz.getAll(name,(int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
//            //获取表中所以的记录数
            int count = serviceVivorz.getCount(name);
            LayData laydata = new LayData();
            laydata.setCode(0);
            laydata.setMsg("");
            laydata.setCount(count);
            laydata.setData(all);
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        } else if ("dlr".equals(opr)) {
            //获取当前页
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            //全查
            List<Vivodenglurizhi> all = serviveVivodlrizhi.getAll((int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
//            //获取表中所以的记录数
            int count = serviveVivodlrizhi.getCount();
            LayData laydata = new LayData();
            laydata.setCode(0);
            laydata.setMsg("");
            laydata.setCount(count);
            laydata.setData(all);
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        } else if ("loginlogs".equals(opr)) {
            String name = req.getParameter("name");
            Date date = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String dates = df.format(date);
            String bt="登录";
            String nr=name+"在"+dates+"进行了登录操作";
String lx="POST";
            int count=serviveVivodlrizhi.add(new Vivodenglurizhi(bt,nr,lx,name,dates));
        }else if ("dlrizhiplsc".equals(opr)) {
            int i = 0;
            String id = req.getParameter("id");
            String[] ids = id.split(",");  //分割id，成一个字符串数组
            int n = ids.length;
            for (int j = 0; j < n; j++) {
                i = serviveVivodlrizhi.del(ids[j]);       //循环执行删除
            }
            flag = false;
            if (i > 0) {
                flag = true;
            }
            resp.getWriter().write(String.valueOf(flag));
        }
    }
}
